package com.rise.serialportdemo.cmd4000.utils;

/**
 * 出钞校验的异或算法
 *
 * 出钞的算法 - 10进制转16进制  16进制转二进制在异或算法得到一个16进制的数组
 *
 */
public class XORUtil {

    public static void main(String[] args) {

        // 07 3A 03 00 00 00 00 00 03
        // 073A11000000000003
        String code = checkcode_0007("073A04000000000003");
        System.out.println("最后：" + code);

    }

    private static String xor(String strHex_X,String strHex_Y){
        //将x、y转成二进制形式
        String anotherBinary=Integer.toBinaryString(Integer.valueOf(strHex_X,16));
        String thisBinary=Integer.toBinaryString(Integer.valueOf(strHex_Y,16));
        String result = "";
        //判断是否为8位二进制，否则左补零
        if(anotherBinary.length() != 8){
            for (int i = anotherBinary.length(); i <8; i++) {
                anotherBinary = "0"+anotherBinary;
            }
        }
        if(thisBinary.length() != 8){
            for (int i = thisBinary.length(); i <8; i++) {
                thisBinary = "0"+thisBinary;
            }
        }
        //异或运算
        for (int i=0;i<anotherBinary.length();i++){
            //如果相同位置数相同，则补0，否则补1
            if(thisBinary.charAt(i)==anotherBinary.charAt(i))
                result+="0"; else{
                result+="1";
            }
        }

        System.out.println("result:"+result);
        return Integer.toHexString(Integer.parseInt(result, 2));
    }

    /**
     *
     * @param para
     * @return
     */
    public static String checkcode_0007(String para){
        String[] dateArr = new String[9];
        try {
            dateArr[0] = para.substring(0, 2);
            dateArr[1] = para.substring(2, 4);
            dateArr[2] = para.substring(4, 6);
            dateArr[3] = para.substring(6, 8);
            dateArr[4] = para.substring(8, 10);
            dateArr[5] = para.substring(10, 12);
            dateArr[6] = para.substring(12, 14);
            dateArr[7] = para.substring(14, 16);
            dateArr[8] = para.substring(16, 18);
            dateArr[9] = para.substring(18, 20);
        }
        catch (Exception e) {
            // TODO: handle exception
        }
        String code = "";
        for (int i = 0; i < dateArr.length-1; i++) {
            if(i == 0){
                code = xor(dateArr[i], dateArr[i+1]);
            } else{
                code = xor(code, dateArr[i+1]);
                System.out.println("code:"+code);
            }
        }
        return code;
    }
}
